/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import db.Redis_Rd;
import db.Redis_W;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.json.simple.JSONValue;
import share.ShareMacros;

/**
 *
 * @author Mrkupi
 */
public class LoadCoteInfo extends javax.swing.JFrame {
    
    private Map<String, String> mValueCote;
    
    public LoadCoteInfo() {
        initComponents();
        setLocation( 500, 200 );
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               dispose();
            }
        });
    }
    
    public LoadCoteInfo( String coteId )
    {
        initComponents();
        setLocation( 500, 200 );
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               dispose();
            }
        });
        
        // Show
        tf_CoteId.setText( coteId );
        btn_LoadMouseClicked(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tf_CoteId = new javax.swing.JTextField();
        btn_Load = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tf_Level = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_Food = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ls_Pengs = new javax.swing.JList();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ls_Eggs = new javax.swing.JList();
        btnShowBoxEgg = new javax.swing.JButton();
        tf_BoxEgg = new javax.swing.JTextField();
        btn_Update = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LOAD COTE INFO");

        jLabel1.setText("CoteID");

        tf_CoteId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_CoteIdKeyPressed(evt);
            }
        });

        btn_Load.setText("Load");
        btn_Load.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_LoadMouseClicked(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cote Info"));

        jLabel2.setText("Level");

        jLabel3.setText("Fish");

        jLabel4.setText("Penguins");

        ls_Pengs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ls_PengsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ls_Pengs);

        jLabel5.setText("Eggs");

        jScrollPane2.setViewportView(ls_Eggs);

        btnShowBoxEgg.setText("Show BoxEgg");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tf_Level)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)))
                    .addComponent(btnShowBoxEgg))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(tf_Food, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(tf_BoxEgg))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_Level, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(tf_Food, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE)
                    .addComponent(jScrollPane2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnShowBoxEgg)
                    .addComponent(tf_BoxEgg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btn_Update.setText("Update");
        btn_Update.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_UpdateMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_CoteId, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_Load)
                        .addGap(0, 173, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btn_Update)
                .addGap(209, 209, 209))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_CoteId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Load))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_Update)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_LoadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_LoadMouseClicked
        
        String keyCote  =   tf_CoteId.getText();
        if( keyCote.equals("") )
            return;
        
        String valueCote        =   Redis_Rd.getInstance().get(keyCote);
        mValueCote  =   (Map<String, String>)JSONValue.parse(valueCote);
        if( mValueCote == null || mValueCote.size() <= 0 )
            return;
        
        tf_Level.setText( String.valueOf(mValueCote.get(ShareMacros.LEVEL)));
        tf_Food.setText( String.valueOf(mValueCote.get(ShareMacros.FISH)));
        
        String keyEggs  =   String.valueOf(mValueCote.get(ShareMacros.EGGS));
        String keyPegs  =   String.valueOf(mValueCote.get(ShareMacros.PEGS));
        
        String keyBoxEgg=   String.valueOf(mValueCote.get(ShareMacros.BOXEGG));
        tf_BoxEgg.setText( keyBoxEgg );
        
        // Load list egg
        DefaultListModel eggModel   = new DefaultListModel();
        Set<String> eggs    =   Redis_Rd.getInstance().smember( keyEggs );
        for( String str : eggs )
        {
            eggModel.addElement( str );
        }
        ls_Eggs.setModel(eggModel);
        
        // Load list peng
        DefaultListModel pegModel   = new DefaultListModel();
        Set<String> pegs    =   Redis_Rd.getInstance().smember( keyPegs );
        for( String str : pegs )
        {
            pegModel.addElement( str );
        }
        ls_Pengs.setModel(pegModel);
        
    }//GEN-LAST:event_btn_LoadMouseClicked

    private void tf_CoteIdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_CoteIdKeyPressed
        if( evt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER )
        {
            btn_LoadMouseClicked( null );
        }
    }//GEN-LAST:event_tf_CoteIdKeyPressed

    private void ls_PengsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ls_PengsMouseClicked
        if (evt.getClickCount() == 2) 
        {
            String selected =   (String)ls_Pengs.getSelectedValue();
            LoadPenguinInfo lPeng   =   new LoadPenguinInfo( selected );
            lPeng.setVisible( true );
        }
    }//GEN-LAST:event_ls_PengsMouseClicked

    private void btn_UpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_UpdateMouseClicked
        
        if( tf_CoteId.getText().equals("") || tf_Level.getText().equals("") || tf_Food.getText().equals("") )
            return;
        
        mValueCote.put(ShareMacros.LEVEL, tf_Level.getText());
        mValueCote.put(ShareMacros.FISH, tf_Food.getText());
        
        if( Redis_W.getInstance().set( tf_CoteId.getText(), JSONValue.toJSONString(mValueCote) ) >= 0 )
            JOptionPane.showMessageDialog(this, "Done");
        
    }//GEN-LAST:event_btn_UpdateMouseClicked

//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(LoadCoteInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(LoadCoteInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(LoadCoteInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(LoadCoteInfo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new LoadCoteInfo().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnShowBoxEgg;
    private javax.swing.JButton btn_Load;
    private javax.swing.JButton btn_Update;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList ls_Eggs;
    private javax.swing.JList ls_Pengs;
    private javax.swing.JTextField tf_BoxEgg;
    private javax.swing.JTextField tf_CoteId;
    private javax.swing.JTextField tf_Food;
    private javax.swing.JTextField tf_Level;
    // End of variables declaration//GEN-END:variables
}
